﻿/*****************************************************************************//*!
* file  : can filter.c
* creat : apleilx
* data  : 2017/11/05
*******************************************************************************/

#include "os_obj.h"
#include "pincfg.h"
#include "sys_init.h"
#include "com_obj.h"
#include "common.h"
#include "bsp_hal.h"
#include "can_task.h"

void can_filter_cfg(void);

/*******************************************************************************
* @brief  can_filter table
*******************************************************************************/
typedef struct
{
	uint8_t moudle;
	uint8_t site;
	uint8_t ide;
	uint8_t fifo_sel;
	
	uint32_t id_comp;
	uint32_t id_mask;
} can_flt_cfg_type;

//- 模式定义
#define MCAN1		1
#define MCAN2		2

#define ID_EXT		1
#define ID_STD		0

#define FIFO0		0
#define FIFO1		1

//- 为简化操作 STD ID使用16bit模式滤波器，EXT ID使用32bit模式滤波器
const can_flt_cfg_type can_flt_cfg[] = 
{
	// CAN1 过滤器
	{MCAN1, 0x00, ID_EXT, FIFO0, 0x00330000UL, 0x00FF0000UL},
	{MCAN1, 0x01, ID_EXT, FIFO1, 0x00440000UL, 0x00FF0000UL},
	{MCAN1, 0x02, ID_EXT, FIFO0, 0x00550000UL, 0x00FF0000UL},
	{MCAN1, 0x03, ID_EXT, FIFO1, 0x00660000UL, 0x00FF0000UL},
	
	{MCAN1, 0x04, ID_STD, FIFO0, 0x01000200UL, 0x07000700UL},
	{MCAN1, 0x05, ID_STD, FIFO1, 0x03000400UL, 0x07000700UL},
	
	// CAN2 过滤器
	{MCAN2, 0x00, ID_EXT, FIFO0, 0x00550000UL, 0x00FF0000UL},
	{MCAN2, 0x01, ID_EXT, FIFO1, 0x00660000UL, 0x00FF0000UL},
	
	{MCAN2, 0x02, ID_STD, FIFO0, 0x01000200UL, 0x07000700UL},
	{MCAN2, 0x03, ID_STD, FIFO1, 0x03000400UL, 0x07000700UL},	
};


/*******************************************************************************
* @brief  can_filter_cfg
* \param[in] none
* \retval: none
*******************************************************************************/
void can_filter_cfg(void)
{
	CAN_FilterInitType cfg;
	dword_tt flt_cid, flt_msk;
	uint8_t index;
	
	cfg.CAN_FilterActivation = ENABLE;
	cfg.CAN_FilterMode = CAN_FilterMode_IdMask;

	for(index=0; index< sizeof(can_flt_cfg)/sizeof(can_flt_cfg_type) ;index++)
	{
		//flt site
		cfg.CAN_FilterNumber = can_flt_cfg[index].site;
		
		//fifo select
		if(can_flt_cfg[index].fifo_sel == 0)
			cfg.CAN_FilterFIFOAssignment = CAN_Filter_FIFO0;
		else
			cfg.CAN_FilterFIFOAssignment = CAN_Filter_FIFO1;
		
		//bit mode
		if(can_flt_cfg[index].ide == ID_EXT)
		{
			cfg.CAN_FilterScale = CAN_FilterScale_32bit;
			flt_cid.uVal = (can_flt_cfg[index].id_comp << 3) | BIT_MASK(2);
			flt_msk.uVal = (can_flt_cfg[index].id_mask << 3) | BIT_MASK(2);
		}
		else
		{
			cfg.CAN_FilterScale = CAN_FilterScale_16bit;
			flt_cid.uVal = (can_flt_cfg[index].id_comp << 5);
			flt_msk.uVal = (can_flt_cfg[index].id_mask << 5) | BIT_MASK(2) | (BIT_MASK(2) << 16);
		}

		cfg.CAN_FilterIdLow = flt_cid.w[0];
		cfg.CAN_FilterIdHigh = flt_cid.w[1];
		cfg.CAN_FilterMskIdLow = flt_msk.w[0];
		cfg.CAN_FilterMskIdHigh = flt_msk.w[1];
		
		if(can_flt_cfg[index].moudle == MCAN2)
			CAN_FilterInit(CAN2, &cfg);	
		else
			CAN_FilterInit(CAN1, &cfg);	
	}
}
